#include "Game.h"


// methods
Boards getBoards()
{
	return myCheckers;
}

// melakukan pembacaan file bitmap
GLuint loadBMP(const char * filename)
{
	AUX_RGBImageRec *TextureImage;
	GLuint tempbmp;
	// membuka file bitmap
	FILE * bmpfile = fopen(filename, "r");
	if (!bmpfile)
	{
		cout<<"Image tidak bisa dibuka"<<endl;
	}
	else if (bmpfile)
	{
		fclose(bmpfile);
		// cout<<"keclose"<<endl;
		memset(TextureImage,0,sizeof(void *)*1);
		TextureImage = auxDIBImageLoad(filename);
		// cout<<"keload"<<endl;
		glGenTextures(1, &tempbmp);			// Create the texture
		// cout<<"kegen"<<endl;
		glBindTexture(GL_TEXTURE_2D, tempbmp);
		// cout<<"kebind"<<endl;
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		// cout<<"kebentuk"<<endl;
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		// cout<<"keparametri"<<endl;
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		// cout<<"keparametri"<<endl;
		// cek apakah texture image eksis
		if (TextureImage)
		{
			if (TextureImage->data)
			{
				free(TextureImage->data);
			}

			free(TextureImage);
		}
		// cout<<"keload"<<endl;
		return tempbmp;
	}

	return 0;
}

void initGL()
{
	// std::cout<<"masuk"<<endl;
	glClearColor( 0.0, 0.0, 0.0, 0.0 );
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, 1.0, 0.1, 100.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
}

// load the image from folder "images", and returns the id of the texture
void loadContent(ThemeState themeState)
{
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_TEXTURE_2D);

	backgroundStartMenu_id = loadBMP("images/startmenu.bmp");
	if (themeState == classic)
	{
		backgroundOptions_id = loadBMP("images/klasik/options.bmp");
		//cout<<"load1?"<<endl;
		backgroundHelp_id = loadBMP("images/klasik/help.bmp");
		//cout<<"load2?"<<endl;
		backgroundCredits_id = loadBMP("images/klasik/credits.bmp");

		// load content for gameplay
		//cout<<"load3?"<<endl;
		papanselected_id = loadBMP("images/klasik/papan_klasik_selected.bmp");
		//cout<<"load4?"<<endl;
		posibilitas_id = loadBMP("images/klasik/possibilities.bmp");
		//cout<<"load5?"<<endl;
		bidak1_id = loadBMP("images/klasik/bidak1.bmp");
		//cout<<"load6?"<<endl;
		bidak2_id = loadBMP("images/klasik/bidak2.bmp");
		//cout<<"load7?"<<endl;
		backgroundGamePlay_id = loadBMP("images/klasik/gameplay.bmp");
		//backgroundHighScore_id = loadBMP(stringtemphighscore);
		//cout<<"load6?"<<endl;
		bidak1_id_pressed = loadBMP("images/klasik/bidak1_pressed.bmp");
		//cout<<"load7?"<<endl;
		bidak2_id_pressed = loadBMP("images/klasik/bidak2_pressed.bmp");
		// cout<<"load8?"<<endl;
		// region kerjaan mp
		backgroundHighScore_id = loadBMP("images/klasik/highscoreboard.bmp");
		pvp_id = loadBMP("images/klasik/orang-orang.bmp");
		pvc_id = loadBMP("images/klasik/orang-comp.bmp");
        cvc_id = loadBMP("images/klasik/comp-comp.bmp");
		// endregion kerjaan mp
	}
	else if (themeState == unyu)
	{
		backgroundOptions_id = loadBMP("images/unyu/options.bmp");
		backgroundHelp_id = loadBMP("images/unyu/help.bmp");
		backgroundCredits_id = loadBMP("images/unyu/credits.bmp");
		papan_id = loadBMP("images/unyu/boardunyu.bmp");
		// cout<<"image papan keload?"<<endl;
		papanselected_id = loadBMP("images/unyu/board_unyu_selected.bmp");
		posibilitas_id = loadBMP("images/unyu/possibilities.bmp");
		bidak1_id = loadBMP("images/unyu/bidak1.bmp");
		bidak2_id = loadBMP("images/unyu/bidak2.bmp");
		backgroundGamePlay_id = loadBMP("images/unyu/boardunyu.bmp");
		backgroundHighScore_id = loadBMP("images/unyu/highscoreboard.bmp");
	}
}

void initialize()
{
	// region kerjaan mp
	// init state
	gameState = options;
	themeState = classic;
    inputState = player1;
	selectPlayerState = pvp;

	player1name = " ";
	player2name = " ";
	length = 0;
	length1 = 0;
	length2 = 0;
	// endregion kerjaan mp

	initGL();
	loadContent(themeState);
	loadFile = "lastGame.dat";
	saveFile = "lastGame.dat";	
	initGame = false;
	endGame = false;
}

void clearPossibleArea(){
	possibleAreaToMove[0][0] = 0;
	possibleAreaToMove[0][1] = 0;
	possibleAreaToMove[0][2] = 0;
	possibleAreaToMove[0][3] = 0;
	possibleAreaToMove[0][4] = 0;
}

float widthRatio(int width, int gameWidth)
{
	return (float) width / gameWidth;
}

float heightRatio(int height, int gameHeight)
{
	return (float) height / gameHeight;
}

// region kerjaan mp
void keyPressed(unsigned char key, int x, int y)
{
	if (key == 27) // jika tombol escape ditekan
	{
         exit(0);
	}
	else if (key == 8) // jika tombol backspace ditekan
	{
		if  (length >= 0)
		{
			--length;
			if (inputState == player1)
			{
				player1name[length] = ' ';
				length1 = length;
			}
			else if (inputState == player2)
			{
				player2name[length] = ' ';
				length2 = length;
			}
		}
		else
		{
			length = 0;
		}
	}
	else
	{
		if(length < 10)
		{
			if (inputState == player1)
			{
				player1name[length] = (char)key;
				length1 = length + 1;
			}
			else if (inputState == player2)
			{
				player2name[length] = (char)key;
				length2 = length + 1;
			}

			++length;
		}
		else
		{
        	length = 10;
		}
	}
}

void mouseButton(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		std::cout<<endl<<"x ="<<x<<",y ="<<y;
		if (gameState == startmenu)
		{
			gameState = gameplay;
		   // jika tombol main baru ditekan
		   if (278 <= x && x <= 553 && 403 <= y && y <= 450)
		   {
				gameState = options;
		   }

		   // jika tombol lanjutkan ditekan
		   if ((75 <= x && x <= 244 && 555 <= y && y <= 620) || (244 <= x && x <= 382 && 536<= y && y <= 586))
		   {
				//loadData();
				gameState = gameplay;
		   }

		   // jika tombol nilai tertinggi ditekan
		   if ((506 <= x && x <= 584 && 501 <= y && y <= 552) || (584 <= x && x <= 663 && 483 <= y && y <= 537) || (663 <= x && x <= 756 && 473 <= y && y <= 523) || (756 <= x && x <= 823 && 464 <= y && y <= 517) || (823 <= x && x <= 842 && 483 <= y && y <= 509))
		   {
				gameState = highscore;
		   }

		   // jika tombol keluar ditekan
		   if (821 <= x && x <= 1115 && 39 <= y && y <= 103)
		   {
           		exit(0);
		   }
		}
		else if (gameState == gameplay){
			if (!endGame){
				int moveTo=0;
				if ((turn ==1)||(turn ==2)){			
					if ((myCheckers.isForceEnemy(turn)) && (!enemyFront)){ // jika harus memakan lawan dan baru pertama kali
						enemyFront = true; // penanda bahwa harus memakan lawan
						currentCoin = 9999; // penanda bahwa baru pertama kali harus memakan lawan
						//cout<<endl<<"harus makan!!!";
					}
					clearPossibleArea();
					coinSelected = getCoinFromMouse(x, y);
					if (coinSelected!=0){
						if (myCheckers.ableToMove(coinSelected)){
							possibleAreaToMove[0][0]=coinSelected;
							if (myCheckers.ableToMove(coinSelected, 1)){				
								// std::cout<<endl<<"ada coin yang terseleksi dan bisa melangkah ke 1";
								possibleAreaToMove[0][1]=1;
							}
							if (myCheckers.ableToMove(coinSelected, 2)){				
								// std::cout<<endl<<"ada coin yang terseleksi dan bisa melangkah ke 2";
								possibleAreaToMove[0][2]=1;
							}
							if (myCheckers.ableToMove(coinSelected, 3)){				
								// std::cout<<endl<<"ada coin yang terseleksi dan bisa melangkah ke 3";
								possibleAreaToMove[0][3]=1;
							}
							if (myCheckers.ableToMove(coinSelected, 4)){				
								// std::cout<<endl<<"ada coin yang terseleksi dan bisa melangkah ke 4";
								possibleAreaToMove[0][4]=1;
							}
						}
						// if (enemyFront){
							// myCheckers.passCoinTo(currentCoin, moveTo);
							// if (!myCheckers.stillForceEnemy(currentCoin)){ // jika sudah tidak bisa memakan lawan lagi
								// playerTurn(); // ganti pemain
								// enemyFront = false; // penanda dimatikan agar tidak memakan lawan lagi
							// }
							// else{
								// //cout<<endl<<"harus makan lagi WOIII ga ganti TURN!!!";
							
							// }
						// }
						// else{
							// myCheckers.moveCoinTo(currentCoin, moveTo);
							// playerTurn(); // ganti pemain
						// }
					}
					else{
						// if (possibleAreaToMove[0][0]!=0){
							// if (() && ()){
							
							// }
							// else if (){
							
							// }
							// else if (){
							
							// }
							// else if (){

							// }
							
						// }
						// clearPossibleArea();
					}					
					
				}

			}
			else{
				std::cout<<endl<<"game sudah berakhir";
			}
		}
		else if (gameState == options || gameState == help || gameState == credits)
		{
        	// region kerjaan mp
			if (970 <= x && x <= 1024 && 66 <= y && y <= 260)
			{
				gameState = options;
			}
			else if (970 <= x && x <= 1024 && 265 <= y && y <= 459)
			{
				gameState = help;
			}
			else if (970 <= x && x <= 1024 && 461 <= y && y <= 655)
			{
             	gameState = credits;
			}

            // jika tombol Main diklik
			if (360 <= x && x <= 651 && 630 <= y && y <= 717 && gameState == options)
			{
             	gameState = gameplay;
			}

			if (247 <= x && x <= 763 && gameState == options)
			{
            	// jika yang diklik adalah label P1
				if (82 <= y && y <= 131)
				{
					inputState = player1;
					length = length1;
				}
				// jika yang diklik adalah label P2
				else if (157 <= y && y <= 206)
				{
					 inputState = player2;
					 length = length2;
				}
			}

			// radio button
			if (245 <= x && x <= 368 && 249 <= y && y <= 316 && gameState == options)
			{
				selectPlayerState = pvp;
			}
			else if (429 <= x && x <= 564 && 249 <= y && y <= 316 && gameState == options)
			{
				selectPlayerState = pvc;
			}
			else if (618 <= x && x <= 763 && 259 <= y && y <= 301 && gameState == options)
			{
				selectPlayerState = cvc;
			}
			// endregion kerjaan mp
		}
	}

	glutPostRedisplay();
}
// endregion kerjaan mp

void updateGamePlay(void)
{
	updateBoards();
	checkersRules();
	glutPostRedisplay();
}
void updateBoards(){
//	std::cout<<"boards"<<endl;
	
}
void checkersRules(){
	//std::cout<<"checkers"<<endl;
	if (gameState == startmenu){
	
	}
	else if (gameState == gameplay){
		if (!endGame){
			if (!initGame){
				bool flag;
				int confirmLoad;
				int modeLoad;
				int turnLoad;			
				bool enemyLoad;			
				comLevel = 1;
				// init game
				
				//masih kasar
				myCheckers.initArea();

				boardSize = 8;
				coinSelected = 0; // 0 artinya tidak 
				clearPossibleArea();
				
				// cout<<endl<<"continue game ? (1/0) = ";
				// cin>>confirmLoad;
				// if (confirmLoad==1){
					// mHistory.LoadLastGame(&flag, loadFile, myCoin, &modeLoad, &turnLoad, &enemyLoad);
				// }
				// else{
					// flag = false;
				// }
				flag = false;
				if (flag){
					turn = turnLoad;
					battleMode = modeLoad;
					enemyFront = enemyLoad;
					cout<<endl<<"berhasil load last game";
					cout<<endl<<"coin Area = ";
					// for (int z=0;z<=23;z++){
						// cout<<endl<<myCoin[z].getAlive();			
					// }
				}
				else{
					// cout<<endl<<"tidak ada last game saved";
					turn = 1;
					battleMode = 1;
					enemyFront = false;
					myCheckers.initCoin();
				}
				myCheckers.fillArea();
				initGame = true;
			}
			else{
				int moveTo=0;
				if ((turn ==3)||(turn ==4)){			
					if ((myCheckers.isForceEnemy(turn)) && (!enemyFront)){ // jika harus memakan lawan dan baru pertama kali
						enemyFront = true; // penanda bahwa harus memakan lawan
						currentCoin = 9999; // penanda bahwa baru pertama kali harus memakan lawan
						//cout<<endl<<"harus makan!!!";
					}
					comAIMovement(&moveTo);
					
					if (enemyFront){
						myCheckers.passCoinTo(currentCoin, moveTo);
						if (!myCheckers.stillForceEnemy(currentCoin)){ // jika sudah tidak bisa memakan lawan lagi
							playerTurn(); // ganti pemain
							enemyFront = false; // penanda dimatikan agar tidak memakan lawan lagi
						}
						else{
							//cout<<endl<<"harus makan lagi WOIII ga ganti TURN!!!";
						
						}
					}
					else{
						myCheckers.moveCoinTo(currentCoin, moveTo);
						playerTurn(); // ganti pemain
					}
				}

			//moveCoin();		
			}
			
		}
		else {
			
		}
	}
}
void playerTurn(){
	 if (battleMode == 1){ // multi player
		if(turn==1){
			turn = 2;
		}
		else if (turn==2) { 
						turn = 1;
			 }
	 }
	 else if (battleMode == 2){ // player vs AI
		if(turn==1){
			turn = 3;
		}
		else if (turn==3) {
						turn = 1;
			 }
//
	 }
	 else if (battleMode == 3){ // AI vs AI
				//
		if(turn==3){
			turn = 4;
		}
		else if (turn==4) {
			turn = 3;
			
		 }
	 }
}
void comAIMovement(int *moveTo){
	switch(comLevel){
	
		case 1: // greedy by side
			/*
			AI pada level satu merupakan Greedy by side. Algoritma ini mengutamakan keberadaan coin checkers agar selalu ada di samping papan
			*/
			if (enemyFront){ // jika harus memakan lawan
				if (currentCoin==9999){ // jika baru pertama kali berhadapan dengan lawan
					// cari koin yang bisa makan di paling depan
					currentCoin = myCheckers.frontKiller(turn);
					cout<<endl<<"coin yang dipilih untuk membunuh= "<<currentCoin;
					(*moveTo) = myCheckers.frontPassKill(currentCoin);
					cout<<endl<<"langkah yang dipilih untuk membunuh= "<<(*moveTo);
				}
				else{ // jika harus memakan lawan lagi
					// cari arah makan yang paling di samping papan
					(*moveTo) = myCheckers.frontPassKill(currentCoin);
					cout<<endl<<"langkah yang dipilih untuk membunuh lagi= "<<(*moveTo);
				}
			}
			else{
				currentCoin = myCheckers.sideCoin(turn);
				cout<<endl<<"coin yang dipilih untuk melangkah= "<<currentCoin;
				(*moveTo) = myCheckers.sideMove(currentCoin);
				cout<<endl<<"langkah yang dipilih = "<<(*moveTo);
				if ((*moveTo)==0){
					cout<<"##########################################################################"<<endl<<endl<<"STOP!!!"<<endl<<endl;
				}
				
			}
			// coin = mostSideCoin(); // memilih koin yang prioritas utama berada di paling samping dan masih dapat bergerak ke samping
			// moveTo = mostSideGoal(); // memutuskan arah gerak untuk menuju ke paling samping papan

			break;
	 case 2: //greedy by kiri payeun and kill the most
			// /*
			// AI pada level dua merupakan Greedy yang mengutamakan gerak bidak paling depan untuk ke kiri, sedangkan bila dihadapkan dengan lawan dipilih bidak untuk memakan lawan paling banyak
			// */
			if (enemyFront){ // jika harus memakan lawan
				if (currentCoin==9999){ // jika baru pertama kali berhadapan dengan lawan
					// cari koin yang bisa makan di paling depan
					currentCoin = myCheckers.coinMostKill();
					cout<<endl<<"coin yang dipilih untuk membunuh= "<<currentCoin;
					(*moveTo) = myCheckers.moveMostKill();
					cout<<endl<<"langkah yang dipilih untuk membunuh= "<<(*moveTo);
				}
				else{ // jika harus memakan lawan lagi
					// cari arah makan yang paling di samping papan
					(*moveTo) = myCheckers.moveMostKill();
					cout<<endl<<"langkah yang dipilih untuk membunuh lagi= "<<(*moveTo);
				}
			}
			else{
				currentCoin = myCheckers.furthestCoinToMove();
				cout<<endl<<"coin yang dipilih untuk melangkah= "<<currentCoin;
				(*moveTo) = myCheckers.leftMove(currentCoin);
				cout<<endl<<"langkah yang dipilih = "<<(*moveTo);					
			}
			break;
	 case 3: //greedy by kiri payeun almost
			// /*
			// AI pada level tiga merupakan Greedy yang mengutamakan pemilihan koin paling depan untuk bergerak ke kiri, termasuk ketika berhadapan dengan lawan dipilih bidak yang paling depan lalu kiri
			// */
			if (enemyFront){ // jika harus memakan lawan
				if (currentCoin==9999){ // jika baru pertama kali berhadapan dengan lawan
					// cari koin yang bisa makan di paling depan
					currentCoin = myCheckers.furthestCoinToKill();
					cout<<endl<<"coin yang dipilih untuk membunuh= "<<currentCoin;
					(*moveTo) = myCheckers.leftKill(currentCoin);
					cout<<endl<<"langkah yang dipilih untuk membunuh= "<<(*moveTo);
				}
				else{ // jika harus memakan lawan lagi
					// cari arah makan yang paling di samping papan
					(*moveTo) = myCheckers.leftKill(currentCoin);
					cout<<endl<<"langkah yang dipilih untuk membunuh lagi= "<<(*moveTo);
				}
			}
			else{
				currentCoin = myCheckers.furthestCoinToMove();
				cout<<endl<<"coin yang dipilih untuk melangkah= "<<currentCoin;
				(*moveTo) = myCheckers.leftMove(currentCoin);
				cout<<endl<<"langkah yang dipilih = "<<(*moveTo);
				
			}
			
			break;

	}
}
void confirmSave(){
	int save;
	cout<<endl<<"save or without save? (1/0) = ";
	cin>>save;
	if (save==1){
		mHistory.SaveLastGame(saveFile, myCheckers.allCoin(), battleMode, turn, enemyFront, 0);
	}
	else{
		mHistory.SaveLastGame(saveFile, myCheckers.allCoin(), battleMode, turn, enemyFront, 1);
	}
}
	void playerMovement(int *moveTo){
		int dir;
		if (enemyFront){ // jika harus memakan lawan
			if (currentCoin==9999){ // jika baru pertama kali berhadapan dengan lawan
				// cari koin yang bisa makan di paling depan
				cout<<endl<<"pilihan coin = ";
				cin>>currentCoin;
				cout<<endl<<"makan coin! Mempass lawan ke (1:kiri atas, 2: kanan atas, 3: kanan bawah, 4: kiri bawah) = ";
				cin>>dir;

			}
			else{ // jika harus memakan lawan lagi
				// cari arah makan yang paling di samping papan
				cout<<endl<<"makan coin lagi! Mempass lawan ke (1:kiri atas, 2: kanan atas, 3: kanan bawah, 4: kiri bawah) = ";
				cin>>dir;
			}
		}
		else{
			cout<<endl<<"pilihan coin = ";
			cin>>currentCoin;
			cout<<endl<<"langkahkan coin ke (1:kiri atas, 2: kanan atas, 3: kanan bawah, 4: kiri bawah) = ";
			cin>>dir;
		}
		if (currentCoin==1234){
			confirmSave();
		}
		(*moveTo) = dir;
				
	}
int getCoinFromMouse(int x, int y){
	int result = 0;
	int posX, posY;
	int startX = 275;
	int startY = 20;
	posX = (x-startX)/90;
	posY = (y-startY)/85;
	result = myCheckers.getCoinNumberFromArea(posX, posY);
	std::cout<<endl<<result;
	return result;
}
int* getAreaFromMouse(int x, int y){
	int result[2];
	int posX, posY;
	int startX = 275;
	int startY = 20;
	posX = (x-startX)/90;
	posY = (y-startY)/85;
	result[0] = posX;
	result[1] = posY;
	std::cout<<endl<<result;
	return result;
}

// region kerjaan mp
void renderBitmapString(float x, float y, void *font, char *string)
{
	char *c;
	glRasterPos2f(x, y);
	for (c=string; *c != '\0'; c++)
	{
		glutBitmapCharacter(font, *c);
	}
}
// endregion kerjaan mp

void drawStartMenu()
{
	glBindTexture(GL_TEXTURE_2D, backgroundStartMenu_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.414f, -0.414f); // Bottom left of the texture and quad
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.414, -0.414f); // Bottom right of the texture and quad
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.414, 0.414); // Top right of the texture and quad
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.414, 0.414); // Top left of the texture and quad
	glEnd();
	//std::cout<<"masuk drawstartmenu"<<endl;
}

// void drawBidak(int bidaknumber, int x, int y)
// {
	// if (0 <= bidaknumber && bidaknumber <= 7)
	// {
		// glBindTexture(GL_TEXTURE_2D, bidak1_id);
		// glBegin(GL_QUADS);
			// glTexCoord2f(0.0f, 0.0f); glVertex2f((-0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (-0.414f + 2.9 - (y * 0.83)) * heightRatio(91, 768)); //draw images dimulai dari kiri bawah
			// glTexCoord2f(1.0f, 0.0f); glVertex2f((0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (-0.414f + 2.9 - (y * 0.83)) * heightRatio(91, 768));   // dilanjutkan ke kanan bawah
			// glTexCoord2f(1.0f, 1.0f); glVertex2f((0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (0.414 + 2.9 - (y * 0.83)) * heightRatio(91, 768));     // setelah itu ke kanan atas
			// glTexCoord2f(0.0f, 1.0f); glVertex2f((-0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (0.414 + 2.9 - (y * 0.83)) * heightRatio(91, 768));    // dan berakhir di kiri atas
		// glEnd();
	// }
	// else if (8<= bidaknumber && bidaknumber <= 15)
	// {
		// glBindTexture(GL_TEXTURE_2D, bidak2_id);
		// glBegin(GL_QUADS);
			// glTexCoord2f(0.0f, 0.0f); glVertex2f((-0.414f - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (-0.414f + 2.9 - (y * 0.83)) * heightRatio(91, 768)); //draw images dimulai dari kiri bawah
			// glTexCoord2f(1.0f, 0.0f); glVertex2f((0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (-0.414f + 2.9 - (y * 0.83)) * heightRatio(91, 768));   // dilanjutkan ke kanan bawah
			// glTexCoord2f(1.0f, 1.0f); glVertex2f((0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (0.414 + 2.9 - (y * 0.83)) * heightRatio(91, 768));     // setelah itu ke kanan atas
			// glTexCoord2f(0.0f, 1.0f); glVertex2f((-0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (0.414 + 2.9 - (y * 0.83)) * heightRatio(91, 768));    // dan berakhir di kiri atas
		// glEnd();
	// }
// }
void drawBidak()
{
	float deltaX = 0.83;
	float deltaY = 0.83;
	float startX =  - 1.75;
	float startY = 2.9;
	int ratio =91;
	int getX, getY;
	for (int b=1;b<=24;b++){
		getX = myCheckers.getCoin(b).getX();
		getY = myCheckers.getCoin(b).getY();
		//std::cout<<endl<<"bidak "<<b<<", x="<<x<<",y="<<y<<endl;
		if (b<=12){
			if (bidakPressed == b){
				glBindTexture(GL_TEXTURE_2D, bidak1_id_pressed);
			
			}
			else{
				glBindTexture(GL_TEXTURE_2D, bidak1_id);
			}
		}
		else{
			if (bidakPressed == b){
				glBindTexture(GL_TEXTURE_2D, bidak2_id_pressed);			
			}
			else{
				glBindTexture(GL_TEXTURE_2D, bidak2_id);
			}
		}
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f); glVertex2f((-0.414f + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (-0.414f +startY - (deltaY*getY)) * heightRatio(ratio, 768)); //draw images dimulai dari kiri bawah
			glTexCoord2f(1.0f, 0.0f); glVertex2f((0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (-0.414f +startY - (deltaY*getY)) * heightRatio(ratio, 768));   // dilanjutkan ke kanan bawah
			glTexCoord2f(1.0f, 1.0f); glVertex2f((0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (0.414 +startY - (deltaY*getY)) * heightRatio(ratio, 768));     // setelah itu ke kanan atas
			glTexCoord2f(0.0f, 1.0f); glVertex2f((-0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (0.414 +startY - (deltaY*getY)) * heightRatio(ratio, 768));    // dan berakhir di kiri atas

			// glTexCoord2f(0.0f, 0.0f); glVertex2f((-0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (-0.414f + 2.9 - (y * 0.83)) * heightRatio(91, 768)); //draw images dimulai dari kiri bawah
			// glTexCoord2f(1.0f, 0.0f); glVertex2f((0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (-0.414f + 2.9 - (y * 0.83)) * heightRatio(91, 768));   // dilanjutkan ke kanan bawah
			// glTexCoord2f(1.0f, 1.0f); glVertex2f((0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (0.414 + 2.9 - (y * 0.83)) * heightRatio(91, 768));     // setelah itu ke kanan atas
			// glTexCoord2f(0.0f, 1.0f); glVertex2f((-0.414 - 0.91 + (((x + (x % 2)) / 2) * 1.66) - (((x + 1) % 2) * 1.66)) * widthRatio(91, 1024), (0.414 + 2.9 - (y * 0.83)) * heightRatio(91, 768));    // dan berakhir di kiri atas
		glEnd();
	
	}
}

void drawOptions()
{
	// region kerjaan mp
	renderBitmapString(-0.20, 0.29,GLUT_BITMAP_TIMES_ROMAN_24, (char*)player1name.c_str());
	renderBitmapString(-0.20, 0.21,GLUT_BITMAP_TIMES_ROMAN_24, (char*)player2name.c_str());

	float x = 0;
	float y = 0;
	int xratio = 163;
	int yratio = 107;
	if (selectPlayerState == pvp)
	{
		glBindTexture(GL_TEXTURE_2D, pvp_id);
		x = 0.0934;
		y = 0.0954;
	}
	if (selectPlayerState == pvc)
	{
		glBindTexture(GL_TEXTURE_2D, pvc_id);
		x = 0.1706;
		y = 0.1272;
	}
	if (selectPlayerState == cvc)
	{
		glBindTexture(GL_TEXTURE_2D, cvc_id);
		x = 0.2498;
		y = 0.1234;
	}

	- 0.414 + x, 0.414 - y
231 , 236   0.0934, 0.0954
422, 238   0.1706, 0.1272
618, 229   0.2498, 0.1234

-0.3206, 0.3186
-0.2434, 0.2868
-0.1642, 0.2906

0.0659, 0.5768

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f((-x) * widthRatio(xratio, 1024), (-y) * heightRatio(yratio, 768)); //draw images dimulai dari kiri bawah
		glTexCoord2f(1.0f, 0.0f); glVertex2f((x) * widthRatio(xratio, 1024), (-y) * heightRatio(yratio, 768));   // dilanjutkan ke kanan bawah
		glTexCoord2f(1.0f, 1.0f); glVertex2f((x) * widthRatio(xratio, 1024), (y) * heightRatio(yratio, 768));     // setelah itu ke kanan atas
		glTexCoord2f(0.0f, 1.0f); glVertex2f((-x) * widthRatio(xratio, 1024), (y) * heightRatio(yratio, 768));    // dan berakhir di kiri atas
	glEnd();

	// endregion kerjaan mp

	glBindTexture(GL_TEXTURE_2D, backgroundOptions_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.414f, -0.414f);	// Bottom left of the texture and quad
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.414, -0.414f); // Bottom right of the texture and quad
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.414, 0.414); // Top right of the texture and quad
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.414, 0.414); // Top left of the texture and quad
	glEnd();
}

void drawHelp()
{
	glBindTexture(GL_TEXTURE_2D, backgroundHelp_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.414f, -0.414f);	// Bottom left of the texture and quad
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.414, -0.414f); // Bottom right of the texture and quad
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.414, 0.414); // Top right of the texture and quad
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.414, 0.414); // Top left of the texture and quad
	glEnd();
}

void drawCredits()
{
	glBindTexture(GL_TEXTURE_2D, backgroundCredits_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.414f, -0.414f); // Bottom left of the texture and quad
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.414, -0.414f); // Bottom right of the texture and quad
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.414, 0.414); // Top right of the texture and quad
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.414, 0.414); // Top left of the texture and quad
	glEnd();
}

void drawGamePlay()
{
	// mengikat texture

	//draw ableMove
	drawBoardsFeatures();

	// draw bidak
	drawBidak();

	// draw board
	glBindTexture(GL_TEXTURE_2D, backgroundGamePlay_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.414f, -0.414f); //draw images dimulai dari kiri bawah
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.414, -0.414f);   // dilanjutkan ke kanan bawah
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.414, 0.414);     // setelah itu ke kanan atas
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.414, 0.414);    // dan berakhir di kiri atas
	glEnd();

	//glBindTexture(GL_TEXTURE_2D, backgroundGamePlay_id);
	//glBindTexture(GL_TEXTURE_2D, papan_id);
	//glBindTexture(GL_TEXTURE_2D, papanselected_id);
	//glBindTexture(GL_TEXTURE_2D, arah_id);
	//glBindTexture(GL_TEXTURE_2D, posibilitas_id);

	//std::cout<<"masuk drawgameplay"<<endl;
}
void drawBoardsFeatures(){
	float deltaX = 0.83;
	float deltaY = 0.83;
	float startX =  - 1.75;
	float startY = 2.9;
	int ratio =91;
	int getX, getY;

		std::cout<<endl<<"Possible nih = "<<possibleAreaToMove[0][0];
		if (possibleAreaToMove[0][0]!=0){
			for (int a=1;a<=4;a++){
				getX = myCheckers.getCoin(possibleAreaToMove[0][0]).getX();
				getY = myCheckers.getCoin(possibleAreaToMove[0][0]).getY();
				if (possibleAreaToMove[0][a]==1){
					if (a==1){
						getX-=1;
						getY-=1;
					}
					else if (a==2){
						getX+=1;
						getY-=1;
					}
					else if (a==3){
						getX+=1;
						getY+=1;
					}
					else if (a==4){
						getX-=1;
						getY+=1;
					}
			// std::cout<<endl<<"masuk posible"<<getX<<getY;
				glBindTexture(GL_TEXTURE_2D, posibilitas_id);
				glBegin(GL_QUADS);
					glTexCoord2f(0.0f, 0.0f); glVertex2f((-0.414f + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (-0.414f +startY - (deltaY*getY)) * heightRatio(ratio, 768)); //draw images dimulai dari kiri bawah
					glTexCoord2f(1.0f, 0.0f); glVertex2f((0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (-0.414f +startY - (deltaY*getY)) * heightRatio(ratio, 768));   // dilanjutkan ke kanan bawah
					glTexCoord2f(1.0f, 1.0f); glVertex2f((0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (0.414 +startY - (deltaY*getY)) * heightRatio(ratio, 768));     // setelah itu ke kanan atas
					glTexCoord2f(0.0f, 1.0f); glVertex2f((-0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (0.414 +startY - (deltaY*getY)) * heightRatio(ratio, 768));    // dan berakhir di kiri atas
				glEnd();
				}
			}
		}
		if (coinSelected!=0){
			getX = myCheckers.getCoin(coinSelected).getX();
			getY = myCheckers.getCoin(coinSelected).getY();
			// std::cout<<endl<<"coin = "<<coinSelected;
			if (coinSelected<=12){
				glBindTexture(GL_TEXTURE_2D, bidak1_id_pressed);
			}
			else{
				glBindTexture(GL_TEXTURE_2D, bidak2_id_pressed);
			}
			glBegin(GL_QUADS);
				glTexCoord2f(0.0f, 0.0f); glVertex2f((-0.414f + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (-0.414f +startY - (deltaY*getY)) * heightRatio(ratio, 768)); //draw images dimulai dari kiri bawah
				glTexCoord2f(1.0f, 0.0f); glVertex2f((0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (-0.414f +startY - (deltaY*getY)) * heightRatio(ratio, 768));   // dilanjutkan ke kanan bawah
				glTexCoord2f(1.0f, 1.0f); glVertex2f((0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (0.414 +startY - (deltaY*getY)) * heightRatio(ratio, 768));     // setelah itu ke kanan atas
				glTexCoord2f(0.0f, 1.0f); glVertex2f((-0.414 + startX + (deltaX*getX)) * widthRatio(ratio, 1024), (0.414 +startY - (deltaY*getY)) * heightRatio(ratio, 768));    // dan berakhir di kiri atas
			glEnd();

		}

}

// region kerjaan mp
void drawHighScore()
{
    int i = 0;
	char coinleft[10][10], time[10][10];
	for (i = 0; i < 10; i++)
	{
		itoa(history.GetPlayer(i).getCoinLeft(), coinleft[i], 10);
		itoa(history.GetPlayer(i).getTime(), time[i], 10);
	}

	// draw header
	renderBitmapString(-0.3, 0.05 * 11 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "Nama Pemain");//(const char*)history.GetPlayer(2).getTime());
	renderBitmapString(0, 0.05 * 11 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "Koin Sisa");//(const char*)history.GetPlayer(3).getTime());
	renderBitmapString(0.3, 0.05 * 11 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "Waktu");//(const char*)history.GetPlayer(4).getTime());

	renderBitmapString(-0.3, 0.05 * 10 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "WWWWWWWWWWWWWWWWWWWW");
	renderBitmapString(-0.3, 0.05 * 9 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes2");//(const char*)history.GetPlayer(1).getTime());
	renderBitmapString(-0.3, 0.05 * 8 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes3");//(const char*)history.GetPlayer(2).getTime());
	renderBitmapString(-0.3, 0.05 * 7 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes4");//(const char*)history.GetPlayer(3).getTime());
	renderBitmapString(-0.3, 0.05 * 6 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes5");//(const char*)history.GetPlayer(4).getTime());
	renderBitmapString(-0.3, 0.05 * 5 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes6");//(const char*)history.GetPlayer(5).getTime());
	renderBitmapString(-0.3, 0.05 * 4 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes7");//(const char*)history.GetPlayer(6).getTime());
	renderBitmapString(-0.3, 0.05 * 3 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes8");//(const char*)history.GetPlayer(7).getTime());
	renderBitmapString(-0.3, 0.05 * 2 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes9");//(const char*)history.GetPlayer(8).getTime());
	renderBitmapString(-0.3, 0.05 * 1 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, "tes10");//(const char*)history.GetPlayer(9).getTime());

	renderBitmapString(0, 0.05 * 10 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[1]);
	renderBitmapString(0, 0.05 * 9 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[2]);//(const char*)history.GetPlayer(1).getTime());
	renderBitmapString(0, 0.05 * 8 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[3]);//(const char*)history.GetPlayer(2).getTime());
	renderBitmapString(0, 0.05 * 7 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[4]);//(const char*)history.GetPlayer(3).getTime());
	renderBitmapString(0, 0.05 * 6 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[5]);//(const char*)history.GetPlayer(4).getTime());
	renderBitmapString(0, 0.05 * 5 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[6]);//(const char*)history.GetPlayer(5).getTime());
	renderBitmapString(0, 0.05 * 4 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[7]);//(const char*)history.GetPlayer(6).getTime());
	renderBitmapString(0, 0.05 * 3 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[8]);//(const char*)history.GetPlayer(7).getTime());
	renderBitmapString(0, 0.05 * 2 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[9]);//(const char*)history.GetPlayer(8).getTime());
	renderBitmapString(0, 0.05 * 1 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, coinleft[10]);//(const char*)history.GetPlayer(9).getTime());

	renderBitmapString(0.3, 0.05 * 10 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[1]);
	renderBitmapString(0.3, 0.05 * 9 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[2]);//(const char*)history.GetPlayer(1).getTime());
	renderBitmapString(0.3, 0.05 * 8 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[3]);//(const char*)history.GetPlayer(2).getTime());
	renderBitmapString(0.3, 0.05 * 7 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[4]);//(const char*)history.GetPlayer(3).getTime());
	renderBitmapString(0.3, 0.05 * 6 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[5]);//(const char*)history.GetPlayer(4).getTime());
	renderBitmapString(0.3, 0.05 * 5 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[6]);//(const char*)history.GetPlayer(5).getTime());
	renderBitmapString(0.3, 0.05 * 4 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[7]);//(const char*)history.GetPlayer(6).getTime());
	renderBitmapString(0.3, 0.05 * 3 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[8]);//(const char*)history.GetPlayer(7).getTime());
	renderBitmapString(0.3, 0.05 * 2 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[9]);//(const char*)history.GetPlayer(8).getTime());
	renderBitmapString(0.3, 0.05 * 1 - 0.3, GLUT_BITMAP_TIMES_ROMAN_24, time[10]);//(const char*)history.GetPlayer(9).getTime());

	cout<<"masuk highscore"<<endl;
	glBindTexture(GL_TEXTURE_2D, backgroundHighScore_id);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.414f, -0.414f); // Bottom left of the texture and quad
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.414, -0.414f); // Bottom right of the texture and quad
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.414, 0.414); // Top right of the texture and quad
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.414, 0.414); // Top left of the texture and quad
	glEnd();
}
// endregion kerjaan mp

void draw()
{   // Clear Color and Depth Buffers
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	// load ulang image-image yang ada
	//loadContent(themeState);
	// Reset transformations
	glLoadIdentity();
	glEnable(GL_TEXTURE_2D);
	glTranslatef(0, 0, -1); // translasi ke sumbu-Z negatif
	if (gameState == startmenu)
	{
		  drawStartMenu();
	}
	else if (gameState == gameplay)
	{
		  drawGamePlay();
	}
	else if (gameState == options)
	{
		drawOptions();
	}
	else if (gameState == help)
	{
		drawHelp();
	}
	else if (gameState == credits)
	{
		drawCredits();
	}
	else if (gameState == highscore)
	{
    	cout<<"highscore?"<<endl;
		drawHighScore();
	}

	glutSwapBuffers();
}

void initregister()
{
	int gameWidth = 1024;
	int gameHeight = 768;

	// register callbacks
	glutDisplayFunc(draw);
	glutIdleFunc(updateGamePlay);
	glutReshapeWindow(gameWidth, gameHeight);

	//glutIgnoreKeyRepeat(0);
	glutKeyboardFunc(keyPressed);
	glutMouseFunc(mouseButton);
	//glutMotionFunc(mouseMove);

	// OpenGL init
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutCreateWindow("Checkers");
	glutPositionWindow((glutGet (GLUT_SCREEN_WIDTH) - 1024) / 2, (glutGet (GLUT_SCREEN_HEIGHT) - 768 + 24)/2); // membuat posisi windows jadi "centered"
	history.initHighScore(100);
	initialize();
	initregister();
	//cout<<gameState<<endl;
	PlaySound("Data/Freeze.wav", NULL, SND_ASYNC);
	glutMainLoop();
	//system("pause");
	//history.printcmd();
	//system("pause");
	return 0;
}
